package com.dy.controller;

import com.dy.entity.Cinema;
import com.dy.entity.Order;
import com.dy.entity.Sale;
import com.dy.entity.SnowIdUtils;
import com.dy.service.IFeignESServlet;
import com.dy.service.IFeignSellService;
import com.dy.vo.SaleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: dy
 * @Date: 2022/4/21 - 04 - 21 - 17:44
 * @Description: com.dy.controller
 * @version: 1.0
 */
@RestController
@Api(tags = "购物车操作")
@RequestMapping("/redis")
public class RedisCinemaServlet {
    @Qualifier("com.dy.service.IFeignSellService")
    @Autowired
    private IFeignSellService feignSellService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Qualifier("com.dy.service.IFeignESServlet")
    @Autowired
    private IFeignESServlet feignESServlet;

    @GetMapping("/redisCinema")
    public List<Sale> redisCinema(){
        List<Sale> list = (List<Sale>) redisTemplate.opsForValue().get("sale");
        if (list == null){
            List<Sale> list1 = feignSellService.redisCinema();
            redisTemplate.opsForValue().set("sale",list1,5, TimeUnit.SECONDS);
            System.out.println("缓存不得");
            return feignSellService.redisCinema();
        }else {
            System.out.println("缓存有");
            return list;
        }
    }

    @PostMapping("/shopping")
    @ApiOperation("/添加购物车")
    public String shopping(SaleVo saleVo){
//        Map<String,Sale> map = (Map<String, Sale>) redisTemplate.opsForHash().get("shopping","map");
//        if (map == null){
//            map = new HashMap<>();
//        }
//        sale.setSaleId((long) map.size());
//        map.put(sale.getSaleId()+"",sale);
//        redisTemplate.opsForHash().put("shopping","map",map);
//        return "添加成功";
        System.out.println(saleVo.getUserName()+"  "+saleVo.getSaleId());
        try {
            redisTemplate.opsForHash().put(saleVo.getUserName(),saleVo.getSaleId()+"car",saleVo);
            return "添加成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败";
        }
    }

    @GetMapping("/delete/{userName}/{shopId}")
    @ApiOperation("/删除购物车单个物品")
    public String delete(@PathVariable("userName")String userName,@PathVariable("shopId")String shopId){
//        Map<String,Sale> map = (Map<String, Sale>) redisTemplate.opsForHash().get("shopping","map");
//        map.remove(shopId);
//        redisTemplate.opsForHash().put("shopping","map",map);
        try {
            redisTemplate.opsForHash().delete(userName,shopId+"car");
            System.out.println(redisTemplate.opsForHash().entries(userName));
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }

    @GetMapping("/selectShopping/{userName}")
    @ApiOperation("/查询购物车物品")
    public Collection<Object> selectShopping(@PathVariable("userName")String userName){
//        Map<String,Sale> map = (Map<String, Sale>) redisTemplate.opsForHash().get("shopping", "map");
//        Collection<Sale> values = map.values();
//        return values;
        redisTemplate.delete(userName+"Z");
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(userName);
        Collection<Object> values = entries.values();
        Iterator<Object> it = values.iterator();
        while (it.hasNext()){
            Object next = it.next();
            redisTemplate.opsForZSet().add(userName+"Z",next,next.hashCode());
        }
        return redisTemplate.opsForZSet().reverseRange(userName+"Z",0,-1);
    }

    @PostMapping("/updateNum")
    public String updateNum(SaleVo saleVo){
        try {
            redisTemplate.opsForHash().delete(saleVo.getUserName(), saleVo.getSaleId()+"car");
            redisTemplate.opsForHash().put(saleVo.getUserName(),saleVo.getSaleId()+"car",saleVo);
            return "修改成功";
        } catch (Exception e) {
            return "修改失败";
        }
    }

    @PostMapping("/shakedown")
    @Transactional
    public boolean shakedown(@RequestBody Order order){
        if (feignSellService.tickets(order.getCinameName())){
            redisTemplate.opsForHash().put("shakedown",order.getOrderSerial(),order);
            if (redisTemplate.opsForHash().hasKey("shakedown",order.getOrderSerial())){
                return true;
            }else {
                //为抢票成功加入es
                feignESServlet.esorder(order);
            }
        }
        return false;
    }

    @GetMapping("/succeed/{serial}")
    public String succeed(@PathVariable("serial")String serial){
        Boolean aBoolean = redisTemplate.opsForHash().hasKey("shakedown", serial);
        if (aBoolean){
            return "抢票成功";
        }
        return "抢票失败";
    }
}
